package com.link.base.tencent.corpwx.externalcontact.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import com.link.base.tencent.corpwx.externalcontact.dao.mybatis.mapper.ExternalFollowTagMapper;
import com.link.base.tencent.corpwx.externalcontact.model.ExternalFollowTag;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 外部联系人-跟进人-标签 
 *
 * @author 欧阳振强
 * @date 2020-01-02 15:54:59.0
 */
@Service
public class ExternalFollowTagServiceImpl extends BasicServiceImpl<ExternalFollowTag> implements ExternalFollowTagService {

    @Resource
    private ExternalFollowTagMapper externalFollowTagMapper;
    
    @Override
    public BasicMapper<ExternalFollowTag> getBasicMapper() {
        return externalFollowTagMapper;
    }

    @Override
    public List<ExternalFollowTag> listExternalFollowTagByHeadId(Long followUserId) throws Exception {
        ExternalFollowTag externalFollowTag = new ExternalFollowTag();
        externalFollowTag.setFollowUserId(followUserId);
        return externalFollowTagMapper.listExternalFollowTagByHeadId(externalFollowTag);
    }

    @Override
    public List<ExternalFollowTag> opExternalFollowTagList(List<ExternalFollowTag> apiList, List<ExternalFollowTag> linkList) throws Exception {
        List<ExternalFollowTag> externalFollowTagList = new ArrayList<>();
        if (apiList == null) {
            apiList = new ArrayList<>();
        }
        if (linkList == null) {
            linkList = new ArrayList<>();
        }
        Map<String, ExternalFollowTag> apiListMap =
            apiList.stream().collect(Collectors.toMap(item -> item.getGroupName() + item.getTagName() + item.getType(), item -> item));
        Map<String, ExternalFollowTag> linkListMap =
            linkList.stream().collect(Collectors.toMap(item -> item.getGroupName() + item.getTagName() + item.getType(), item -> item));
        for (ExternalFollowTag apiItem: apiList) {
            if (linkListMap.get(apiItem.getGroupName() + apiItem.getTagName() + apiItem.getType()) == null) {
                apiItem.setOp("add");
                externalFollowTagList.add(apiItem);
            }
        }
        for (ExternalFollowTag linkItem: linkList) {
            if (apiListMap.get(linkItem.getGroupName() + linkItem.getTagName() + linkItem.getType()) == null) {
                linkItem.setOp("del");
                externalFollowTagList.add(linkItem);
            }
        }
        return externalFollowTagList;
    }

    @Override
    public void insertByList(List<ExternalFollowTag> list, Long followUserId) throws Exception {
        for (ExternalFollowTag item: list) {
            item.setFollowUserId(followUserId);
            item.setId(keyGenerateService.keyGenerate());
            externalFollowTagMapper.insert(item);
        }
    }

    @Override
    public void delByList(List<ExternalFollowTag> list) throws Exception {
        for (ExternalFollowTag item: list) {
            externalFollowTagMapper.deleteById(item);
        }
    }
}